<template>
  <header class="header">
    <div class="navbar">
      <div class="navbar-left-container">
        <a href="/">
          <img class="navbar-brand-logo" src="/static/logo1.png"></a>
      </div>
      <div class="navbar-right-container" style="display: flex;position: relative;left: 90%;">
        <div class="navbar-menu-container">
          <!--<a href="/" class="navbar-link">我的账户</a>-->
          <a href="javascript:void(0)"><img src="../../static/cart.png" height="18" width="18"/></a>
          <span class="navbar-link" v-text="nickName" v-if="nickName"></span>
          <a href="javascript:void(0)" class="navbar-link" @click="loginModalFlag=true" v-if="!nickName">Login</a>
          <a href="javascript:void(0)" class="navbar-link" @click="logout">Logout</a>
          <div class="navbar-cart-container">
          </div>
        </div>
      </div>
    </div>
    <!--登录界面-->
    <div class="md-modal modal-msg md-modal-transition" v-bind:class="{'md-show':loginModalFlag}">
      <div class="md-modal-inner">
        <div class="md-top">
          <div class="md-title">Login in</div>
          <button class="md-close" @click="loginModalFlag=false">Close</button>
        </div>
        <div class="md-content">
          <div class="confirm-tips">
            <div class="error-wrap">
              <span class="error error-show" v-show="errorTip">用户名或者密码错误</span>
            </div>
            <ul>
              <li class="regi_form_input">
                <i class="icon IconPeople"></i>
                <input type="text" tabindex="1" name="loginname" v-model="userName"
                       class="regi_login_input regi_login_input_left" placeholder="User Name" data-type="loginname">
              </li>
              <li class="regi_form_input noMargin">
                <i class="icon IconPwd"></i>
                <input type="password" tabindex="2" name="password" v-model="userPwd"
                       class="regi_login_input regi_login_input_left login-input-no input_text"
                       placeholder="Password"
                       @keyup.enter="login"
                >
              </li>
            </ul>
          </div>
          <div class="login-wrap">
            <a href="javascript:;" class="btn-login" @click="login">登 录</a>
          </div>
        </div>
      </div>
    </div>
    <div class="md-overlay" v-if="loginModalFlag" @click="loginModalFlag=false"></div>
  </header>
</template>

<script>
  import '../assets/login.css'
  import axios from 'axios'

  export default {
    name: "mall-header",
    data() {
      return {
        loginModalFlag: false,
        errorTip: false,
        userName: '',
        userPwd: '',
        nickName: ''//nickName为空时，用户名不显示,login 显示
      }
    },
    methods: {
      login() {
        if (!this.userName || !this.userPwd) {//如果为空
          this.errorTip = true;//显示错误信息
          return;
        }
        axios.post('/api/users/login', {
          userName: this.userName,userPwd: this.userPwd}).then(res=>{
            let resData=res.data;
            if (resData.status==='0'){//请求成功
              this.errorTip=false;
              this.loginModalFlag=false;
              this.nickName=resData.result;//result=userName
              //to do 可以查看浏览器中是否存在userId
              //向父组件发起事件，重新请求数据
              this.$emit('reGet');
            } else {
              this.errorTip=true;
            }
        })
      },
      logout(){
        axios.post('/api/users/logout').then(res=>{
          let resData=res.data;
          if (resData.status==='0'){
            //服务器传来的cookie已经把浏览器中的userId杀掉了
            this.nickName='';
            //向父组件发起事件，重新请求数据
            this.$emit('reGet');
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
